layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 分类商品管理
     */
    var FlowGoods = {
        tableId: "flowGoodsTable"
    };

    /**
     * 初始化表格的列
     */
    FlowGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'goodsId', hide: true, title: 'ID'},
            {field: 'goodsName', sort: true, title: '物品名称'},
            {field: 'typeName', sort: true, title: '物品分类'},
            {field: 'cover', sort: true, title: '物品图片',templet:function (row) {
                    return row.cover ? "<img src='"+row.cover+"' />" : "无";
            }},
            {field: 'goodsPrice', sort: true, title: '费用'},
            {field: 'note', sort: true, title: '备注'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowGoods.search = function () {
        var queryData = {};

        queryData['goodsName'] = $('#goodsName').val();

        table.reload(FlowGoods.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowGoods.onSelectItem = function (data) {
        var iframs = top.document.getElementsByTagName('iframe');
        var goodsPrice = data.goodsPrice || 0;
        for (var i = 0;i<iframs.length;i++){
            if(iframs[i].contentWindow.PFun){
                iframs[i].contentWindow.PFun.chooseShop(data.goodsId,data.goodsName,goodsPrice,data.typeName);
            }
        }
        admin.closeThisDialog();
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowGoods.tableId,
        url: Feng.ctxPath + '/flowOrder/flowGoodsList?storeCityId='+Feng.getUrlParam("storeId"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowGoods.initColumn()
    });

    // 工具条点击事件
    table.on('tool(' + FlowGoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'select') {
            FlowGoods.onSelectItem(data);
        }
    });
});
